package com.example.controller;

import com.example.Service.CourseService;
import com.example.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
@CrossOrigin(origins = "*")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping("/add")
    public String addCourse(@RequestBody Course course) {
        return courseService.addCourse(course) > 0 ? "success" : "fail";
    }

    @DeleteMapping("/delete/{courseId}")
    public String deleteCourse(@PathVariable Integer courseId) {
        return courseService.deleteCourseById(courseId) > 0 ? "success" : "fail";
    }

    @PutMapping("/update")
    public String updateCourse(@RequestBody Course course) {
        return courseService.updateCourse(course) > 0 ? "success" : "fail";
    }

    @GetMapping("/user/{userId}")
    public List<Course> getCoursesByUserId(@PathVariable Integer userId) {
        return courseService.getCoursesByUserId(userId);
    }
}
